#!/bin/bash

source /etc/profile 
if [ $# -eq 1 ]; then
	targetHour=$1
else
	targetHour=`date -d "-1 hours" +"%Y%m%d%H"`
fi
path=$(cd $(dirname $0);pwd)
parentPath=$(dirname ${path})
targetDate=${targetHour:0:8}
if [ ! -d "${path}/${targetDate}" ];then
	mkdir -p ${path}/${targetDate}
fi
day=$(date -d"${targetDate}" "+%Y-%m-%d")

#事件概览
hive -e "set mapred.job.name=[NAS][event][overview][${targetDate}];use formatlog;insert overwrite local directory '${path}/${targetDate}/${targetHour}_event_overview' select cid,sid,${targetDate},1,0,count(1) as pv,count(distinct nasid) as uv,count(1)/count(distinct nasid) from nas_tracking_format_hour where day=${targetDate} and eventid is not null and length(eventid)>0 group by cid,sid"
#事件趋势
hive -e "set mapred.job.name=[NAS][event][each][${targetDate}];use formatlog;insert overwrite local directory '${path}/${targetDate}/${targetHour}_event_each' select cid,sid,${targetDate},2,eventid,count(1) as pv,count(distinct nasid) as uv,count(1)/count(distinct nasid) from nas_tracking_format_hour where day=${targetDate} and eventid is not null and length(eventid)>0 group by cid,sid,eventid"
wait
# 合并结果文件
cat ${path}/${targetDate}/${targetHour}_event_overview/* ${path}/${targetDate}/${targetHour}_event_each/* > ${path}/${targetDate}/${targetHour}_event
wait
# load data to mysql
source ${parentPath}/configs/mysql_config
python ${path}/load_event_to_mysql.py ${path}/${targetDate}/${targetHour}_event ${host} ${port} ${user} ${password} ${db} ${day}